package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.ItemCategoryMapper;
import cn.tedu.fruitshop.mapper.ItemMapper;
import cn.tedu.fruitshop.pojo.dto.ItemCategoryAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.ItemCategoryUpdateDTO;
import cn.tedu.fruitshop.pojo.entity.ItemCategory;
import cn.tedu.fruitshop.pojo.entity.ItemCategoryTree;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryListItemVO;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryStandardVO;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryTreeVO;
import cn.tedu.fruitshop.service.IItemCategoryService;
import cn.tedu.fruitshop.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.DefaultJerseyApplicationPath;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ItemCategoryServiceImpl implements IItemCategoryService {
    @Autowired
    private ItemCategoryMapper itemCategoryMapper;
    @Autowired
    private ItemMapper itemMapper;


    @Override
    public void addNew(ItemCategoryAddNewDTO itemCategoryAddNewDTO) {
        log.debug("开始处理【添加商品分类】的业务，参数：{}", itemCategoryAddNewDTO);

        int count = itemCategoryMapper.countByName(itemCategoryAddNewDTO.getName());
        if (count != 0) {
            String message = "您输入的商品类别已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        ItemCategoryStandardVO standardById = itemCategoryMapper.getStandardById(itemCategoryAddNewDTO.getTwoItem());
        if (standardById == null) {
            String message = "您输入的商品二级id不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        ItemCategory itemCategory = new ItemCategory();
        BeanUtils.copyProperties(itemCategoryAddNewDTO, itemCategory);
        log.debug("即将插入商品分类数据：{}", itemCategory);
        int rows = itemCategoryMapper.insert(itemCategory);
        if (rows != 1) {
            String message = "服务器网络波动,添加商品分类数据失败,请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入商品分类数据完成");
    }

    @Override
    public void delete(Integer id) {
        ItemCategoryStandardVO queryResult = itemCategoryMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除商品分类失败,尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //从商品列表中查询列表下是否存在商品,如果存在,则不允许删除
        int countByCategoryId = itemMapper.countByCategoryId(id);
        if (countByCategoryId != 0) {
            String message = "删除商品分类失败,该商品列表下存在商品!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        //从商品分类列表查询是否存在二级分类
        int countByParentId = itemCategoryMapper.countByCategoryId(id);
        if (countByParentId != 0) {
            String message = "删除商品分类失败,该商品列表下存在二级分类!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        int rows = itemCategoryMapper.deleteById(id);
        if (rows != 1) {
            String message = "服务器网络波动,删除商品分类数据失败,请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public List<ItemCategoryListItemVO> list() {
        log.debug("开始处理查询分类列表的业务,无参数");

        List<ItemCategoryListItemVO> list = itemCategoryMapper.list();
        if (list == null) {
            String message = "查询商品分类列表失败,服务器响应错误!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return list;
    }

    @Override
    public void updateInfoById(Integer id, ItemCategoryUpdateDTO itemCategoryUpdateDTO) {
        log.debug("开始处理【修改商品分类详情】的业务，参数ID：{}, 新数据：{}", id, itemCategoryUpdateDTO);
        // 检查名称是否被占用

        int count = itemCategoryMapper.countByNameAndNotId(id, itemCategoryUpdateDTO.getName());
        if (count > 0) {
            String message = "修改商品分类失败，商品分类名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }


        ItemCategoryStandardVO queryResult = itemCategoryMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改商品分类详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        ItemCategory itemCategory = new ItemCategory();
        BeanUtils.copyProperties(itemCategoryUpdateDTO, itemCategory);
        itemCategory.setId(id);

        // 修改数据
        log.debug("即将修改数据：{}", itemCategory);
        int rows = itemCategoryMapper.update(itemCategory);
        if (rows != 1) {
            String message = "修改商品分类详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ItemCategoryStandardVO getStandardVO(Integer id) {
        log.debug("开始处理【根据id查询类别列表详情】的业务，参数：{}", id);
        ItemCategoryStandardVO itemCategoryStandardVO = itemCategoryMapper.getStandardById(id);
        if (itemCategoryStandardVO == null) {
            String message = "获取类别列表详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return itemCategoryStandardVO;
    }

    @Override
    public List<ItemCategoryTree> listByParentId() {
        log.debug("开始处理根据parentId查询列表的业务,无参数");


        List<ItemCategoryTree> resultByParentId = itemCategoryMapper.listByParentId();
        if (resultByParentId == null) {
            String message = "服务器网络波动!查询数据失败!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        return resultByParentId;
    }

    @Override
    public List<ItemCategoryTree> listByTwoItem(Integer twoItem) {
        log.debug("开始处理根据parentId查询列表的业务,参数为{}", twoItem);


        List<ItemCategoryTree> resultByTwoItem = itemCategoryMapper.listByTwoItem(twoItem);
        if (resultByTwoItem == null) {
            String message = "服务器网络波动!查询数据失败!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        return resultByTwoItem;
    }

    @Override
    public ItemCategoryTreeVO<ItemCategoryTree> itemCategoryTree() {
        //获取所有一级分类
        List<ItemCategoryTree> firstLevels = itemCategoryMapper.listByParentId();
        log.debug("一级分类为{}",firstLevels);
        //遍历一级分类 把对应的二级分类塞进一级里
        for (ItemCategoryTree oneLevel : firstLevels) {
            Integer secondLevelParentId = oneLevel.getId();
            List<ItemCategoryTree> secondLevels = itemCategoryMapper.listByTwoItem(secondLevelParentId);
            oneLevel.setChildrens(secondLevels);
        }
        ItemCategoryTreeVO<ItemCategoryTree> treeVO = new ItemCategoryTreeVO<>();
        treeVO.setCategories(firstLevels);
        return treeVO;

    }


}
